package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.PhoneUserEntivity;
import com.sam.im.heiqun.uis.adapters.holder.PhoneUserHolder;

import java.util.List;

/**
 * Created by wangguodong on 2017/1/21.
 * 手机通讯录使用
 */
public class PhoneUsersAdpter extends RecyclerView.Adapter<PhoneUserHolder> {
    private static final String TAG = "PhoneUsersAdpter";
    private Context context;


    private boolean showFinish = false;
    private List<PhoneUserEntivity> mFrientEntivities;
    private FriendListClickListener mItemClickListener;
    private int type = 0;//判断选择框是否显示；0-不显示；1-显示;2-不显示

    private IShowMore iShowMore;

    public void setiShowMore(IShowMore iShowMore) {
        this.iShowMore = iShowMore;
    }

    public PhoneUsersAdpter(Context context, List<PhoneUserEntivity> mFrientEntivities) {
        this.context = context;
        this.mFrientEntivities = mFrientEntivities;
    }

    public PhoneUsersAdpter(Context context, List<PhoneUserEntivity> mFrientEntivities, int type) {
        this.context = context;
        this.mFrientEntivities = mFrientEntivities;
        this.type = type;
    }

    public PhoneUsersAdpter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return position < mFrientEntivities.size() ? 0 : 1;
    }


//    public void refresh(List<PhoneUserEntivity> mFrientEntivities) {
//        this.mFrientEntivities.clear();
//        this.mFrientEntivities.addAll(mFrientEntivities);
//        notifyDataSetChanged();
//    }

    public interface FriendListClickListener {
        void onAgreeNewFriendClick(View view, int position, int type);
    }

    public void setItemClickListener(FriendListClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public PhoneUserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = null;
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_phone_user, parent, false);
                break;
            case 1:
                view = LayoutInflater.from(context).inflate(R.layout.item_progress, parent, false);
                break;
        }
        return new PhoneUserHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(PhoneUserHolder holder, int position) {
        if (position == mFrientEntivities.size()) {
            if (iShowMore != null) iShowMore.showMore();
            if (showFinish) {
                TextView textView = (TextView) holder.getmView().findViewById(R.id.item_text);
                textView.setVisibility(View.VISIBLE);
                ProgressBar progressBar = (ProgressBar) holder.getmView().findViewById(R.id.item_progress);
                progressBar.setVisibility(View.GONE);
            }
            return;
        }
//        holder.head_image.setImageBitmap(mFrientEntivities.get(position).getPhone());
//        holder.name.setText(mFrientEntivities.get(position).getMarkName());
//        if ("1".equals(mFrientEntivities.get(position).getIsExists())) {
//            holder.yaoqing.setBackgroundResource(R.drawable.bg_circle_btn_gray);
//        } else {
//            holder.yaoqing.setBackgroundResource(R.drawable.bg_circle_btn);
//        }
    }

    public void setShowFinish(boolean showFinish) {
        this.showFinish = showFinish;
    }

    @Override
    public int getItemCount() {
        return mFrientEntivities != null ? mFrientEntivities.size() + 1 : 0;
    }

    public interface IShowMore {
        void showMore();
    }

}